package com.letwal.cmcvnd.aviewmodel

import androidx.lifecycle.*
import com.letwal.cmcvnd.adata.user.*
import com.letwal.cmcvnd.base.BaseViewModel
import com.letwal.cmcvnd.anetwork.ApiUserReposity
import com.letwal.cmcvnd.data.ReqBase
import com.letwal.cmcvnd.home.repo.HomeCoinManager

@Suppress("COMPATIBILITY_WARNING")
class UserViewModel : BaseViewModel() {

    //////////////////////////////////////////
    private val mGetUserWalletLiveData = MutableLiveData<ReqGetUserWallet>()

    val onGetUserWalletLiveData =
        Transformations.switchMap(mGetUserWalletLiveData) { query ->
            ApiUserReposity.getUserWallet(this, query)
        }

    fun getUserWallet(reqGetUserWallet: ReqGetUserWallet) {
        mGetUserWalletLiveData.value = reqGetUserWallet
    }

    //////////////////////////////////////////
    private val mSetWalletNameLiveData = MutableLiveData<ReqSetWalletName>()

    val onSetWalletNameLiveData =
        Transformations.switchMap(mSetWalletNameLiveData) { query ->
            ApiUserReposity.setWalletName(this, query)
        }

    fun setWalletName(reqSetWalletName: ReqSetWalletName) {
        mSetWalletNameLiveData.value = reqSetWalletName
    }

    //////////////////////////////////////////
    private val mGetUserAccountsLiveData = MutableLiveData<ReqGetUserWallet>()

    private val getUserAccountsLiveData =
        Transformations.switchMap(mGetUserAccountsLiveData) { query ->
            ApiUserReposity.getUserAccounts(this, query)
        }

    val onGetUserAccountsResultLiveData = MutableLiveData<Boolean>()

    fun getUserAccounts(owner: LifecycleOwner, reqGetUserWallet: ReqGetUserWallet) {
        getUserAccountsLiveData.observe(owner) {
            if (it.isSuccess) {
                it.getOrNull()?.let { result ->
                    HomeCoinManager.mAccounts.clear()
                    HomeCoinManager.mAccounts.addAll(result)
                    onGetUserAccountsResultLiveData.value = true
                }
            }
        }
        mGetUserAccountsLiveData.value = reqGetUserWallet
    }

    //////////////////////////////////////////
    private val mGetMyInviteInfoLiveData = MutableLiveData<ReqBase>()

    val onGetMyInviteInfoLiveData =
        Transformations.switchMap(mGetMyInviteInfoLiveData) { query ->
            ApiUserReposity.getMyInviteInfo(this, query)
        }

    fun getMyInviteInfo(reqBase: ReqBase) {
        mGetMyInviteInfoLiveData.value = reqBase
    }

    //////////////////////////////////////////
    val mAllTransactions = mutableListOf<RevDataGetMyTransactions>()
    val mOutTransactions = mutableListOf<RevDataGetMyTransactions>()
    val mInTransactions = mutableListOf<RevDataGetMyTransactions>()

    private val mGetMyTransactionsLiveData = MutableLiveData<ReqGetMyTransactions>()

    val onGetMyTransactionsLiveData =
        Transformations.switchMap(mGetMyTransactionsLiveData) { query ->
            ApiUserReposity.getMyTransactions(this, query)
        }

    fun getMyTransactions(reqGetMyTransactions: ReqGetMyTransactions) {
        reqGetMyTransactions.showLoading = false
        mGetMyTransactionsLiveData.value = reqGetMyTransactions
    }
}